/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.person.address;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.person.address.CountryDetails;

/**
 *
 * @author Gebruiker
 */
@Stateless
public class CountryFacade extends AbstractFacade<Country> implements CountryFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CountryFacade() {
        super(Country.class);
    }
    
    @Override
    public Integer createCountry(String name, String countryCode){
        Country country = new Country(name, countryCode);
        create(country);
        return country.getId();
    }
    
    @Override
    public void editCountry(Integer id, String name, String countryCode){
        Country country = find(id);
        country.setCountryCode(countryCode);
        country.setName(name);
        edit(country);
    }
    
    @Override
    public void removeCountry(Integer id){
        Country country = find(id);
        remove(country);
    }
    
    @Override
    public CountryDetails getCountryDetails(Integer id){
        return convert(find(id));
    }
    
    @Override
    public Collection<CountryDetails> getAllCountryDetails(){
        Collection<CountryDetails> countries = new ArrayList<CountryDetails>();
        for(Country country:findAll()){
            countries.add(convert(country));
        }
        return countries;
    }
    
    public CountryDetails convert(Country country){
        return new CountryDetails(country.getId(), country.getName(), country.getCountryCode());
    }
    
    
    
}
